home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / pyshared / checkbox / report.py < prev    next >
Encoding:
Python Source  |  2009-04-27  |  4.8 KB  |  151 lines

  1. #
  2. # This file is part of Checkbox.
  3. #
  4. # Copyright 2008 Canonical Ltd.
  5. #
  6. # Checkbox is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # Checkbox is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Checkbox.  If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. from xml.dom.minidom import Document, Element, parseString
  20.  
  21.  
  22. class ReportManager(object):
  23.     """The central point for dumping and loading information.
  24.  
  25.     This keeps references to all reports which have been added to the
  26.     instance of this manager. These reports will be able to register
  27.     handlers to understand the formats for dumping and loading actions.
  28.     """
  29.  
  30.     def __init__(self, name, version=None, stylesheet=None):
  31.         self.name = name
  32.         self.version = version
  33.         self.stylesheet = stylesheet
  34.         self.dumps_table = {}
  35.         self.loads_table = {}
  36.         self.document = None
  37.  
  38.     def handle_dumps(self, type, handler):
  39.         """
  40.         Call back method for reports to register dump handlers.
  41.         """
  42.         if type in self.dumps_table:
  43.             raise Exception, "Dumps type already handled: %s" % type
  44.         self.dumps_table[type] = handler
  45.  
  46.     def handle_loads(self, type, handler):
  47.         """
  48.         Call back method for reports to register load handlers.
  49.         """
  50.         if type in self.loads_table:
  51.             raise Exception, "Loads type already handled: %s" % type
  52.         self.loads_table[type] = handler
  53.  
  54.     def call_dumps(self, obj, node):
  55.         """
  56.         Convenience method for reports to call the dump handler
  57.         corresponding to the type of the given object.
  58.         """
  59.         return self.dumps_table[type(obj)](obj, node)
  60.  
  61.     def call_loads(self, node):
  62.         """
  63.         Convenience method for reports to call the load handler
  64.         corresponding to the content of the given node.
  65.         """
  66.         if self.loads_table.has_key(node.localName):
  67.             ret = self.loads_table[node.localName](node)
  68.         elif isinstance(node, Element) and node.hasAttribute("type"):
  69.             type = node.getAttribute("type")
  70.             ret = self.loads_table[type](node)
  71.         else:
  72.             ret = self.loads_table["default"](node)
  73.         return ret
  74.  
  75.     def add(self, report):
  76.         """
  77.         Add a new report to the manager.
  78.         """
  79.         report.register(self)
  80.  
  81.     def dumps(self, obj):
  82.         """
  83.         Dump the given object which may be a container of any objects
  84.         supported by the reports added to the manager.
  85.         """
  86.         self.document = Document()
  87.  
  88.         if self.stylesheet: 
  89.             type = "text/xsl"
  90.             href = "file://%s" % self.stylesheet
  91.             style = self.document.createProcessingInstruction("xml-stylesheet",
  92.                 "type=\"%s\" href=\"%s\"" % (type, href))
  93.             self.document.appendChild(style)
  94.  
  95.         node = self.document.createElement(self.name)
  96.         self.document.appendChild(node)
  97.  
  98.         if self.version:
  99.             node.setAttribute("version", self.version)
  100.  
  101.         try:
  102.             self.call_dumps(obj, node)
  103.         except KeyError, e:
  104.             raise ValueError, "Unsupported type: %s" % e
  105.  
  106.         document = self.document
  107.         self.document = None
  108.         return document
  109.  
  110.     def loads(self, str):
  111.         """
  112.         Load the given string which may be a container of any nodes
  113.         supported by the reports added to the manager.
  114.         """
  115.         document = parseString(str)
  116.         node = document.childNodes[0]
  117.         assert(node.localName == self.name)
  118.  
  119.         try:
  120.             ret = self.call_loads(document)
  121.         except KeyError, e:
  122.             raise ValueError, "Unsupported type: %s" % e
  123.  
  124.         return ret
  125.  
  126.  
  127. class Report(object):
  128.     """A convenience for writing reports.
  129.  
  130.     This provides a register method which will set the manager attribute
  131.     and optionally call the C{register_dumps} and C{register_loads}
  132.     methods.
  133.     """
  134.  
  135.     def _create_element(self, name, parent):
  136.         element = self._manager.document.createElement(name)
  137.         parent.appendChild(element)
  138.         return element
  139.  
  140.     def _create_text_node(self, text, parent):
  141.         text_node = self._manager.document.createTextNode(text)
  142.         parent.appendChild(text_node)
  143.         return text_node
  144.  
  145.     def register(self, manager):
  146.         self._manager = manager
  147.         if hasattr(self, "register_dumps"):
  148.             self.register_dumps()
  149.         if hasattr(self, "register_loads"):
  150.             self.register_loads()
  151.